﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;

namespace PlanningDepartment
{
    public partial class FrDangNhap : Telerik.WinControls.UI.RadForm
    {
        public FrDangNhap()
        {
            InitializeComponent();
        }
        private void ChangeThemeName(Control control, string themeName)
        {
            IComponentTreeHandler radControl = control as IComponentTreeHandler;
            if (radControl != null) { radControl.ThemeName = themeName; }
            foreach (Control child in control.Controls)
            { ChangeThemeName(child, themeName); }
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            ChangeThemeName(this, "Office2010Black");
        }

        private void lnk_Thoat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

        private void bt_DangNhap_Click(object sender, EventArgs e)
        {
            Process dta = new Process();
            string sql = @"select UsersName,Password from Users";
            //if (dta.check_input(sql, txt_UserName.Text, 0) && dta.check_input(sql, txt_Password.Text, 1))
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                cmd.CommandText = sql;
                SqlDataReader rd = cmd.ExecuteReader();
                int i = 0;
                while (rd.Read())
                {
                    if (txt_UserName.Text == rd[0].ToString() && txt_Password.Text == rd[1].ToString())
                    {
                        MessageBox.Show("ĐĂNG NHẬP THÀNH CÔNG");
                        FrMain FrM = this.MdiParent as FrMain;
                        FrM.Show();
                        FrM.User(txt_UserName.Text);
                        FrM.Enable(txt_UserName.Text);
                        //FrMain FrM = new FrMain();
                        
                       // MessageBox.Show(txt_UserName.Text);
                        //FrM.Show();
                       // FrM.User(txt_UserName.Text);
                        
                       // FrM.Enable(txt_UserName.Text);
                        //MessageBox.Show("aaaaa");
                        rd.Close();
                       
                        //MessageBox.Show(date);
                        string update = @"UPDATE [dbo].[Users] SET DateAccess=getdate() where UsersName='" + txt_UserName.Text + "'";
                        cmd.CommandText = update;
                        cmd.ExecuteNonQuery();
                        i = 0;
                        break;
                    }
                    else
                        i++;
                }
                if (i > 0)
                {
                    MessageBox.Show("Thông tin không chính xác");
                    txt_UserName.Focus();
                }
                else
                {
                    /*txt_UserName.Text = "";
                    txt_Password.Text = "";*/
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        

        private void FrDangNhap_KeyDown_1(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
                SendKeys.Send("{TAB}");
        }
    }
}
